#ifndef BOARD_H
#define BOARD_H

#include <../nav/types.h>

/* #define STM32F10X_LD */   /*!< STM32F10X_LD: STM32 Low density devices */
#define STM32F10X_MD    /*!< STM32F10X_MD: STM32 Medium density devices */
/* #define STM32F10X_HD */   /*!< STM32F10X_HD: STM32 High density devices */


/* Uncomment the line below to enable peripheral header file inclusion */
/* #include "stm32f10x_adc.h" */
/* #include "stm32f10x_bkp.h" */
/* #include "stm32f10x_can.h" */
/* #include "stm32f10x_crc.h" */
/* #include "stm32f10x_dac.h" */
/* #include "stm32f10x_dbgmcu.h" */
/* #include "stm32f10x_dma.h" */
/* #include "stm32f10x_exti.h" */
/* #include "stm32f10x_flash.h" */
#include "stm32f10x_fsmc.h"
#include "stm32f10x_gpio.h"
/* #include "stm32f10x_i2c.h" */
/* #include "stm32f10x_iwdg.h" */
/* #include "stm32f10x_pwr.h" */
#include "stm32f10x_rcc.h"
/* #include "stm32f10x_rtc.h" */
/* #include "stm32f10x_sdio.h" */
/* #include "stm32f10x_spi.h" */
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
/* #include "stm32f10x_wwdg.h" */
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */

//------------------------  
// Leds Definition   
//------------------------  
#define LED1                PC12
#define LED2                PC13
#define NB_LED              2
#define LED_MASK            (LED1|LED2)

//------------------------  
// I2C   
//------------------------  
#define I2C_SDA             PB11
#define I2C_SCL             PB10

//------------------------  
// Pressure sensor   
//------------------------  
#define HP03D_XCLR          PC15
#define HP03D_MCLK          PA1

//-------------------------  
// GPS USART 
//-------------------------  
#define GPS_RXD             PA3
#define GPS_TXD             PA2
#define GPS_PORT            2
#define GPS_BAUD            19200

//-------------------------  
// Receiver PWM inputs   
//-------------------------  

#define RECEIVER1_GPIO_PORT         GPIOB
#define RECEIVER1_PIN               GPIO_Pin_2      // PB2
#define RECEIVER1_TIM_PORT          TIM3
#define RECEIVER1_CH                TIM_Channel_3   // TIM3_CH3
#define RECEIVER2_GPIO_PORT         GPIOB
#define RECEIVER2_PIN               GPIO_Pin_1      // PB1
#define RECEIVER2_TIM_PORT          TIM3
#define RECEIVER2_CH                TIM_Channel_4   // TIM3_CH4
#define RECEIVER3_GPIO_PORT         GPIOA
#define RECEIVER3_PIN               GPIO_Pin_8      // PA8
#define RECEIVER3_TIM_PORT          TIM1
#define RECEIVER3_CH                TIM_Channel_1   // TIM1_CH1
#define RECEIVER4_GPIO_PORT         GPIOA
#define RECEIVER4_PIN               GPIO_Pin_0      // PA0
#define RECEIVER4_TIM_PORT          TIM5
#define RECEIVER4_CH                TIM_Channel_1   // TIM5_CH1
#define RECEIVER5_GPIO_PORT         GPIOA
#define RECEIVER5_PIN               GPIO_Pin_10     // PA10
#define RECEIVER5_TIM_PORT          TIM1
#define RECEIVER5_CH                TIM_Channel_3   // TIM1_CH3
#define RECEIVER6_GPIO_PORT         GPIOA
#define RECEIVER6_PIN               GPIO_Pin_9      // PA9
#define RECEIVER6_TIM_PORT          TIM1
#define RECEIVER6_CH                TIM_Channel_2   // TIM1_CH2
#define RECEIVER7_GPIO_PORT         GPIOB
#define RECEIVER7_PIN               GPIO_Pin_4      // PB4
#define RECEIVER7_TIM_PORT          TIM3
#define RECEIVER7_CH                TIM_Channel_1   // TIM3_CH1
#define RECEIVER8_GPIO_PORT         GPIOB
#define RECEIVER8_PIN               GPIO_Pin_5      // PB5
#define RECEIVER8_TIM_PORT          TIM3
#define RECEIVER8_CH                TIM_Channel_2   // TIM3_CH2

#define NUM_RECEIVER_INPUTS 8

//-------------------------  
// Servo outputs   
//-------------------------  
#define SERVO1_4_PORT       GPIOB
#define SERVO1_PIN          GPIO_Pin_6
#define SERVO2_PIN          GPIO_Pin_7
#define SERVO3_PIN          GPIO_Pin_8
#define SERVO4_PIN          GPIO_Pin_9
#define SERVO5_8_PORT       GPIOC
#define SERVO5_PIN          GPIO_Pin_6
#define SERVO6_PIN          GPIO_Pin_7
#define SERVO7_PIN          GPIO_Pin_8
#define SERVO8_PIN          GPIO_Pin_9

#define NUM_SERVO_OUTPUTS 8

//-------------------------  
// Leveler (analog inputs)
//-------------------------  
#define LEVELER_UPDOWN      PC0             // ADC123_IN10
#define LEVELER_A           PC1             // ADC123_IN11
#define LEVELER_B           PC2             // ADC123_IN12
#define NUM_ANALOG_INPUTS   3

//-------------------------  
// USB
//-------------------------  
#define USB_DETECT          PC4
#define USB_PULLUP          PC14

//-------------------------  
// Optional serial radio USART 
//-------------------------  
#define SRADIO_RXD          PC11
#define SRADIO_TXD          PC10
#define SRADIO_PORT         3
#define SRADIO_BAUD         115200

//-------------------------  
// AUXSER USART (available instead of RX5/RX6)
//-------------------------  
#define AUXSER_RXD          PA10
#define AUXSER_TXD          PA9
#define AUXSER_PORT         1
#define AUXSER_BAUD         19200
#define AUXSER_ENABLE       0

#define NUM_SERIAL_CHANNELS 3

//-------------------------  
// microSD card (via SPI1)
//-------------------------  
#define MICROSD_MISO        PA6
#define MICROSD_MOSI        PA7
#define MICROSD_SCK         PA5
#define MICROSD_CS          PC5
#define MICROSD_CD          PA4

//--------------  
// Master Clock   
//--------------  
#define MASTER_CLOCK        72000000
#define PERIPHERAL_CLOCK    (MASTER_CLOCK/2)

/* Exported constants --------------------------------------------------------*/
/* Uncomment the line below to expanse the "assert_param" macro in the 
   Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT    1 */

/* Exported macro ------------------------------------------------------------*/
#ifdef  USE_FULL_ASSERT

/**
  * @brief  The assert_param macro is used for function's parameters check.
  * @param  expr: If expr is false, it calls assert_failed function
  *   which reports the name of the source file and the source
  *   line number of the call that failed. 
  *   If expr is true, it returns no value.
  * @retval None
  */
  #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
  void assert_failed(uint8_t* file, uint32_t line);
#else
  #define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */

#endif /* BOARD_H */
